<template>
  <div>
    <div class="search">
      <a-form ref="formRef" :model="form" :labelCol="labelCol">
        <a-row :gutter="20">
          <a-col :span="6" :md="8" :xs="8" :lg="6">
            <a-form-item label="状态">
              <a-select ref="select" v-model:value="form.status" placeholder="请选择退款状态">
                <a-select-option v-for="item in orderStatus" :value="item.status" :key="item.status">{{ item.title }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="6" :md="8" :xs="8" :lg="6">
            <a-form-item label="订单号">
              <a-input v-model:value="form.number" placeholder="请输入订单号" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="6" :md="8" :xs="8" :lg="6">
            <a-form-item label="来源">
              <a-input v-model:value="form.source" placeholder="请输入来源" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="6" :md="8" :lg="6">
            <a-form-item label="被服务人">
              <a-input v-model:value="form.userName" placeholder="请输入被服务人" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="6" :md="8" :xs="8" :lg="6">
            <a-form-item label="服务名称">
              <a-input v-model:value="form.serviceName" placeholder="请输入服务名称" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="6" :md="8" :xs="8" :lg="6">
            <a-form-item label="护士姓名">
              <a-input v-model:value="form.nurseName" placeholder="请输入护士姓名" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="6" :md="8" :xs="8" :lg="6">
            <a-form-item label="护士ID">
              <a-input v-model:value="form.nurseId" placeholder="请输入护士ID" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="2">
            <a-button type="primary" @click="search" class="d-a-center">搜索</a-button>
          </a-col>
          <a-col :span="2">
            <a-button type="primary" @click="reset" class="d-a-center">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--订单列表 -->
    <a-row style="margin: 20px 0" justify="end" :gutter="10" v-has="{ a: $route, b: 2 }">
      <a-col :span="4" style="text-align: right" :lg="4" :xs="8">
        <a-upload v-model:file-list="fileList" name="file" :customRequest="importOrder" :headers="headers" accept=".xls,xlsx">
          <a-button class="d-a-center">
            <upload-outlined></upload-outlined>
            导入订单
          </a-button>
        </a-upload>
      </a-col>
      <a-col :span="2" :lg="2" :xs="4">
        <a-button type="primary" @click="addNewOrder" class="d-a-center">新增订单</a-button>
      </a-col>
      <a-col :span="2" :lg="2" :xs="4">
        <a-button type="primary" danger @click="handlePay" class="d-a-center">一键结算</a-button>
      </a-col>
      <a-col :span="2" :lg="2" :xs="4">
        <a-button type="primary" warning @click="handleFenpei" class="d-a-center">一键分配</a-button>
      </a-col>
    </a-row>
    <a-table
      rowKey="id"
      :columns="colums"
      :data-source="list"
      bordered
      :loading="loading"
      :pagination="pagination"
      @change="handleTableChange"
      :row-selection="rowSelection"
      :scroll="{ y: 540 }"
    >
      <!-- 订单状态显示 -->
      <template #status="{ record }">
        <a-tag :color="orderListStatusColor(record.status)">
          {{ orderListStatus(record.status) }}
        </a-tag>
      </template>
      <template #price="{ record }">
        <div style="color: red">
          <span>{{ record.price }}</span>
          <edit-outlined v-if="record.status != 5" @click="editPrice(record)" style="margin-left: 5px" v-has="{ a: $route, b: 2 }" />
        </div>
      </template>
      <template #nurseId="{ record }">
        <span>{{ record.nurseId ? record.nurseId : '未分配' }}</span>
      </template>
      <template #realPrice="{ record }">
        <span>{{ record.realPrice ? record.realPrice : '未结算' }}</span>
      </template>
      <template #amount="{ record }">
        <span>{{ record.amount ? record.amount : 0 }}</span>
      </template>
      <template #operation="{ record }">
        <a-button type="link" danger v-if="record.status == 2" @click="divide(record)" v-has="{ a: $route, b: 2 }">分配</a-button>
        <a-button type="link" v-if="record.status != 5" @click="edit(record)" v-has="{ a: $route, b: 2 }">编辑</a-button>
        <a-button type="link" v-has="{ a: $route, b: 1 }" @click="look(record)">查看</a-button>
        <a-button type="link" danger @click="settle(record)" v-if="record.status == 6" v-has="{ a: $route, b: 2 }">结算</a-button>
        <a-button type="link" danger @click="deal(record)" v-if="record.status == 7" v-has="{ a: $route, b: 2 }">处理</a-button>
        <a-button type="link" danger v-if="record.status == 4" @click="confirm(record)" v-has="{ a: $route, b: 2 }">确认</a-button>
      </template>
    </a-table>
    <!-- 新增订单 -->
    <a-modal
      :visible="addOrderVisible"
      :title="isEdit ? '编辑订单' : '新增订单'"
      @ok="addOrder"
      @cancel="closeModal('addOrderVisible')"
      okText="保存"
      width="50%"
    >
      <a-form :model="modalForm" :rules="rules" :labelCol="labelCol" ref="modalFormRef">
        <a-row :gutter="20">
          <a-col :span="12">
            <a-form-item label="来源" name="source">
              <a-input placeholder="请输入订单来源" v-model:value="modalForm.source" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="被服务人" name="userName">
              <a-input placeholder="请输入被服务人" v-model:value="modalForm.userName" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="服务名称" name="serviceName">
              <a-input placeholder="请输入服务名称" v-model:value="modalForm.serviceName" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="服务地址" name="address">
              <a-input placeholder="请输入服务地址" v-model:value="modalForm.address" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="服务日期" name="scheduleTime">
              <a-date-picker
                placeholder="请输入服务日期"
                show-time
                @change="selectServiceTime"
                v-model:value="modalForm.scheduleTime"
                valueFormat="YYYY/MM/DD"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="时间分段" name="workTime">
              <a-input placeholder="请输入时间分段" allow-clear v-model:value="modalForm.workTime"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="护士姓名" name="nurseName">
              <a-input placeholder="请输入护士姓名" v-model:value="modalForm.nurseName" allow-clear></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="应结金额" name="price">
              <a-input placeholder="请输入应结金额" v-model:value="modalForm.price" allow-clear></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>
    <!-- 查看订单 -->
    <a-modal :visible="orderVisible" title="查看" :footer="null" @cancel="closeModal('orderVisible')" width="50%">
      <a-tag color="warning" style="margin-bottom: 20px; padding: 8px">服务信息</a-tag>
      <a-descriptions bordered :column="{ xxl: 2, xl: 2, lg: 2, md: 1, sm: 1, xs: 1 }">
        <a-descriptions-item label="订单号">{{ orderDetail.number }}</a-descriptions-item>
        <a-descriptions-item label="来源">{{ orderDetail.source }}</a-descriptions-item>
        <a-descriptions-item label="服务名称">{{ orderDetail.serviceName }}</a-descriptions-item>
        <a-descriptions-item label="服务地址">{{ orderDetail.address }}</a-descriptions-item>
        <a-descriptions-item label="服务时间">{{ orderDetail.scheduleTime }}</a-descriptions-item>
        <a-descriptions-item label="时间分段">{{ orderDetail.workTime }}</a-descriptions-item>
        <a-descriptions-item label="应结金额">{{ orderDetail.price }}</a-descriptions-item>
        <a-descriptions-item label="实结金额">{{ orderDetail.realPrice ? orderDetail.realPrice : '未结算' }}</a-descriptions-item>

        <a-descriptions-item label="创建时间">{{ orderDetail.createdAt }}</a-descriptions-item>
        <a-descriptions-item label="订单状态">
          <a-tag :color="orderListStatusColor(orderDetail.status)">
            {{ orderListStatus(orderDetail.status) }}
          </a-tag>
        </a-descriptions-item>
        <a-descriptions-item v-if="orderDetail.status != 2" label="分配时间">{{ orderDetail.updatedAt }}</a-descriptions-item>
        <a-descriptions-item v-if="orderDetail.status == 7" label="备注">{{ orderDetail.otherInfo }}</a-descriptions-item>
      </a-descriptions>

      <template v-if="orderDetail.status != 2">
        <a-tag color="warning" style="margin: 20px 0; padding: 8px">护士信息</a-tag>
        <a-descriptions bordered :column="{ xxl: 2, xl: 2, lg: 2, md: 1, sm: 1, xs: 1 }">
          <a-descriptions-item label="护士ID">{{ orderDetail.nurseId }}</a-descriptions-item>
          <a-descriptions-item label="护士姓名">{{ orderDetail.nurseName }}</a-descriptions-item>
          <a-descriptions-item label="联系方式">{{ orderDetail.mobile }}</a-descriptions-item>
          <a-descriptions-item label="地理位置">{{ orderDetail.nurseAddress }}</a-descriptions-item>
          <a-descriptions-item label="注册时间">{{ orderDetail.registerTime }}</a-descriptions-item>
          <a-descriptions-item label="账户余额">{{ orderDetail.amount ? orderDetail.amount : 0 }}</a-descriptions-item>
        </a-descriptions>
      </template>

      <a-form :label-col="labelCol">
        <a-divider></a-divider>
        <a-row justify="space-around">
          <!-- <a-col :span="4" :offset="8" v-if="orderDetail.status == 2">
            <a-button type="primary" @click="openModal('divideNurseVisible')"
              >分配护士</a-button
            >
          </a-col> -->
          <template v-if="!isLook">
            <a-col :span="4" :offset="8" v-if="orderDetail.status == 4">
              <a-button type="primary" @click="openModal('savePriceVisible')">完成</a-button>
            </a-col>
          </template>
          <a-col :span="4">
            <a-button type="info" @click="closeModal('orderVisible')">关闭</a-button>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>
    <!-- 一键结算 -->
    <a-modal :visible="myPriceVisible" title="一键结算" :footer="null" @cancel="closeModal('myPriceVisible')">
      <div style="margin: 20px 40px">
        <div class="items">
          <span class="name">结算单量：</span>
          <span class="valuse">{{ rowSelection.selectedRowKeys.length }}</span>
        </div>
        <div class="items">
          <span class="name">应结金额：</span>
          <span class="valuse">{{ payMount }}</span>
        </div>
        <div class="tips" style="margin-top: 10px">tips:*结算单量:需要结算订单的总数 *应结金额：结算订单的应结之和</div>
        <div class="tips">*只计算“核对成功”的订单状态</div>
        <a-divider></a-divider>
        <a-row>
          <a-col :span="4" :offset="8">
            <a-button type="info" @click="closeModal('myPriceVisible')">关闭</a-button>
          </a-col>
          <a-col :span="4" :offset="2">
            <a-button type="primary" @click="handlePayOrder" :disabled="rowSelection.selectedRowKeys.length == 0">确定</a-button>
          </a-col>
        </a-row>
      </div>
    </a-modal>
    <!-- 一键分配 -->
    <a-modal :visible="myFenpeiVisible" title="一键分配" :footer="null" @cancel="closeModal('myFenpeiVisible')">
      <div style="margin: 20px 40px">
        <div class="items">
          <span class="name">分配单量：</span>
          <span class="valuse">{{ fenpeiList.length }}</span>
        </div>

        <!-- <div class="tips" style="margin-top: 10px">
          tips:*结算单量:需要结算订单的总数 *应结金额：结算订单的应结之和
        </div> -->
        <div class="tips">*只计算“待分配”的订单状态</div>
        <a-divider></a-divider>
        <a-row>
          <a-col :span="4" :offset="8">
            <a-button type="info" @click="closeModal('myFenpeiVisible')">关闭</a-button>
          </a-col>
          <a-col :span="4" :offset="2">
            <a-button type="primary" @click="handleFenPayOrder" :disabled="fenpeiList.length == 0">一键分配</a-button>
          </a-col>
        </a-row>
      </div>
    </a-modal>
    <!-- 一键分配 -->
    <a-modal :visible="divideAllSuccess" title="分配完成" :footer="null" @cancel="closeModal('divideAllSuccess')">
      <div style="margin: 20px 40px">
        <div class="items">
          <span class="name">分配单量：</span>
          <span class="valuse">{{ divideAll.offOrderNum }}</span>
        </div>
        <div class="items">
          <span class="name">分配成功：</span>
          <span class="valuse">{{ divideAll.offOrderSuccess }}</span>
        </div>
        <!-- <div class="tips" style="margin-top: 10px">
          tips:*结算单量:需要结算订单的总数 *应结金额：结算订单的应结之和
        </div> -->
        <div class="tips">*匹配失败的订单请人工手动分配！</div>
        <a-divider></a-divider>
        <a-row>
          <!-- <a-col :span="4" :offset="8">
            <a-button type="info" @click="closeModal('divideAllSuccess')"
              >关闭</a-button
            >
          </a-col> -->
          <!-- <a-col :span="4" :offset="2">
            <a-button
              type="primary"
              @click="closeModal('divideAllSuccess')"
              >确定</a-button
            >
          </a-col> -->
        </a-row>
      </div>
    </a-modal>
    <!-- 结算价格 -->
    <a-modal :visible="savePriceVisible" title="修改应结金额" :footer="null" @cancel="closeModal('savePriceVisible')">
      <div style="margin: 20px 40px">
        <a-form @finish="onFinish" :model="priceForm">
          <a-form-item label="应结金额" name="price" :rules="[{ required: true, message: '请输入结算价格!' }]">
            <a-input-number v-model:value="priceForm.price" placeholder="请选择结算价格" style="width: 200px" :min="0.01"></a-input-number>
          </a-form-item>
          <a-divider></a-divider>
          <a-form-item>
            <a-row>
              <a-col :span="4" :offset="8">
                <a-button type="primary" html-type="submit">保存</a-button>
              </a-col>
            </a-row>
          </a-form-item>
        </a-form>
      </div>
    </a-modal>

    <!-- 修改订单状态 -->
    <a-modal :visible="editVisible" title="" :footer="null" @cancel="closeModal('editVisible')">
      <div style="margin: 20px 40px">
        <a-form :model="editForm" ref="editFormRef" :rules="rules1">
          <!-- <a-form-item label="订单状态" name="status">
            <a-select
              ref="select"
              v-model:value="editForm.status"
              @change="handleChange"
              placeholder="请选择订单状态"
            >
              <a-select-option
                v-for="item in editOrderStatus"
                :value="item.status"
                :key="item.status"
                >{{ item.title }}</a-select-option
              >
            </a-select>
          </a-form-item> -->
          <a-form-item label="应结金额"> {{ orderDetail.price }}元 </a-form-item>
          <a-form-item label="结算金额" name="price">
            <a-row :gutter="20">
              <a-col :span="18">
                <a-input disabled v-model:value="editForm.price" placeholder="请仔细核对结算金额"></a-input>
              </a-col>
              <a-col :span="6">
                <span @click="autoComplete" style="font-size: 12px; color: blue">自动输入</span>
              </a-col>
            </a-row>
          </a-form-item>
        </a-form>
      </div>
      <a-divider></a-divider>
      <a-row>
        <a-col :span="4" :offset="8">
          <a-button type="primary" @click="editSave">保存</a-button>
        </a-col>
        <a-col :span="4" :offset="3">
          <a-button type="info" @click="closeModal('editVisible')">取消</a-button>
        </a-col>
      </a-row>
    </a-modal>
    <a-row>
      <a-col :span="5">
        <a-button type="info" @click="excleData" v-has="{ a: $route, b: 2 }">导出数据</a-button>
      </a-col>
    </a-row>

    <!-- 展示用户导入的数据 -->
    <a-modal
      :visible="importVisible"
      title="导入的订单"
      okText="确认导入"
      @cancel="closeModal('importVisible')"
      @ok="confirmImport"
      width="80%"
    >
      <a-table :dataSource="importDataList" :columns="columns1" />
    </a-modal>

    <!-- 分配护士 -->
    <divide-nurse
      option="offOrder"
      v-if="divideNurseVisible"
      :visible="divideNurseVisible"
      :number="orderDetail.number"
      :offline="true"
      @closeModal="closeModal('divideNurseVisible')"
      @divideNurse="divideNurse"
    ></divide-nurse>
  </div>
</template>
<script>
import { reactive, ref, watch, computed, defineComponent, createVNode, toRefs } from 'vue'
import {
  CheckOutlined,
  EditOutlined,
  ArrowDownOutlined,
  ArrowUpOutlined,
  ExclamationCircleOutlined,
  EditTwoTone,
  DeleteTwoTone,
  UploadOutlined
} from '@ant-design/icons-vue'
import { Table } from 'ant-design-vue'
import { message, Modal, modal } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import xlsx from 'xlsx' //这个最新版本的会报错最好装低版本本
import {
  selectAllNew,
  importExcel,
  saveOrUpdate,
  editOrderPrice,
  selectOne,
  payOrder,
  offlineorder,
  onKeyAllot,
  selectAllOfflineOrderExport,
  selectAllNewIds
} from '@/api/order/index'
import divideNurse from '@/views/order/components/DivideNurse'
import moment from 'moment'
import { deleteObjBlank } from '@/assets/js/common.js'
import { debounce } from '@/assets/js/debounceThrottle.js'
import { useModal, useSelects } from '@/hooks'

// import {
// 	assertArrayTypeAnnotation
// } from "@babel/types";
export default {
  setup() {
    const formRef = ref()
    let form = reactive({})
    const visible = ref(false)
    const data = reactive({
      loading: true,
      orderStatusAct: 0, //订单状态
      time: [], //订单时间
      modalForm: {},
      addOrderVisible: false,
      orderVisible: false,
      isLook: false, //是否是编辑
      editVisible: false,
      editFormRef: '',
      editForm: {},
      modalFormRef: '',
      importDataList: [], //导入的数据是
      importVisible: false, //导入数据弹窗
      list: [],
      number: '', //订单编号
      orderDetail: {}, //订单详情
      divideNurseVisible: false,
      savePriceVisible: false, //结算价格弹窗
      priceForm: {}, //结算价格
      selectList: [], //选中的列表
      isEdit: false,
      num: 0, //导入订单
      fenpeiList: [], //一键分配订单
      payMount: 0,
      myPriceVisible: false,
      myFenpeiVisible: false,
      divideAllSuccess: false,
      divideAll: {},
      selectedRowKeys: []
    })
    const router = useRouter()
    // 分页器
    const page = reactive({
      current: 1,
      pageSize: 10,
      total: 0
    })

    // 订单状态列表
    const orderStatus = [
      {
        title: '全部',
        status: 0
      },
      {
        title: '待分配',
        status: 2
      },
      {
        title: '待核对',
        status: 4
      },
      {
        title: '核对成功',
        status: 6
      },
      {
        title: '核对失败',
        status: 7
      },
      {
        title: '已结算',
        status: 5
      }
    ]

    // 订单状态
    const orderListStatus = status => {
      let title = ''
      switch (status) {
        case 2:
          title = '待分配'
          break
        case 4:
          title = '待核对'
          break
        case 6:
          title = '核对成功'
          break
        case 7:
          title = '核对失败'
          break
        case 5:
          title = '已结算'
          break
        default:
          break
      }
      return title
    }
    // 样式
    const orderListStatusColor = status => {
      let color = ''
      switch (status) {
        case 2:
          color = 'warning'
          break
        case 4:
          color = 'error'
          break
        case 5:
          color = 'success'
          break
        case 6:
          color = 'blue'
          break
        case 7:
          color = 'error'
        default:
          break
      }
      return color
    }

    const editOrderStatus = [
      {
        title: '已完成',
        status: 5
      },
      {
        title: '已取消',
        status: 6
      }
    ]
    const rules = {
      price: [
        {
          required: true,
          message: '请输入应算金额'
        }
      ],
      nurseName: [
        {
          required: true,
          message: '请输入护士姓名',
          trigger: 'blur'
        }
      ],
      serviceName: [
        {
          required: true,
          message: '请输入服务名称',
          trigger: 'blur'
        }
      ],
      userName: [
        {
          required: true,
          message: '请输入被服务人',
          trigger: 'blur'
        }
      ],
      scheduleTime: [
        {
          required: true,
          message: '请选择服务时间'
        }
      ],
      source: [
        {
          required: true,
          message: '请输入来源',
          trigger: 'blur'
        }
      ],
      address: [
        {
          required: true,
          message: '请输入服务地址',
          trigger: 'blur'
        }
      ],
      workTime: [
        {
          required: true,
          message: '请输入工作时间'
        }
      ]
    }
    const rules1 = {
      price: [
        {
          required: true,
          message: '请点击自动输入!'
        }
      ]
    }

    const colums = [
      {
        title: '订单号',
        dataIndex: 'number',
        className: 'table',
        width: 150,
        fixed: 'left'
      },
      {
        title: '来源',
        dataIndex: 'source',
        className: 'table'
      },
      {
        title: '被服务人',
        dataIndex: 'userName',
        className: 'table'
      },
      {
        title: '服务名称',
        dataIndex: 'serviceName',
        className: 'table'
      },
      // {
      //   title: "服务时间",
      //   dataIndex: "scheduleTime",
      //   className: "table",
      // },
      {
        title: '护士姓名',
        dataIndex: 'nurseName',
        className: 'table'
      },
      {
        title: '护士ID',
        dataIndex: 'nurseId',
        className: 'table',
        slots: {
          customRender: 'nurseId'
        }
      },
      {
        title: '应结金额',
        dataIndex: 'price',
        className: 'table',
        slots: {
          customRender: 'price'
        }
      },
      {
        title: '实结金额',
        dataIndex: 'realPrice',
        className: 'table',
        slots: {
          customRender: 'realPrice'
        }
      },
      {
        title: '账户余额',
        dataIndex: 'amount',
        className: 'table',
        slots: {
          customRender: 'amount'
        }
      },

      {
        title: '创建时间',
        dataIndex: 'createdAt',
        className: 'table'
      },
      {
        title: '状态',
        dataIndex: 'status',
        className: 'table',
        slots: {
          customRender: 'status'
        }
      },
      {
        title: '操作',
        dataIndex: 'operation',
        className: 'table',
        fixed: 'right',
        width: 100,
        slots: {
          customRender: 'operation'
        }
      }
    ]
    const columns1 = [
      {
        title: '来源',
        dataIndex: 'source'
      },
      {
        title: '被服务人',
        dataIndex: 'userName'
      },
      {
        title: '服务名称',
        dataIndex: 'serviceName'
      },
      {
        title: '服务地址',
        dataIndex: 'address'
      },
      {
        title: '服务日期',
        dataIndex: 'scheduleTime'
      },
      {
        title: '护士姓名',
        dataIndex: 'nurseName'
      },
      {
        title: '应结金额',
        dataIndex: 'price'
      },
      {
        title: '时间分段',
        dataIndex: 'workTime'
      }
    ]

    const chooseOrderStatus = (status, index) => {
      // console.log(status, index);
      data.orderStatusAct = index
      if (status == 0) {
        delete form['status']
        init()
      } else {
        form.status = status
        init()
      }
    }

    const { renderData, rowSelection } = useSelects({})

    // 分页器
    const pagination = computed(() => ({
      current: page.current,
      pageSize: page.pageSize,
      pageSizeOptions: ['10', '20', '30'],
      showTotal: (total, range) => {
        return '共' + total + '条'
      },
      total: page.total,
      showQuickJumper: true,
      showSizeChanger: true
    }))

    // 分页器改变的时候
    const handleTableChange = (pagination, filters, sorter) => {
      console.log(pagination, '当前分页')
      page.current = pagination.current
      page.pageSize = pagination.pageSize

      // 请求列表接口
      init()
    }
    const selectServiceTime = (e, dateString) => {
      // form.startTime = dateString[0];
      // form.endTime = dateString[1];
      console.log(e, dateString, 'eeeeeeee-')
      data.modalForm.scheduleTime = dateString
    }

    // 导入订单--自定义请求数据
    const importOrder = (file, fileList) => {
      // console.log(file, "file----");
      let reader = new FileReader()
      reader.readAsBinaryString(file.file) //这个是file.file文件，可不是file……
      reader.onload = ev => {
        let res = ev.target.result
        const worker = xlsx.read(res, {
          type: 'binary'
        })
        // console.log(worker, "worker是----");

        // 这个是将数据进行一步拼接
        //  worker.Sheets[worker.SheetNames[0]]

        // 将返回的数据转换为json对象的数据
        reader = xlsx.utils.sheet_to_json(worker.Sheets[worker.SheetNames[0]])
        // console.log(reader, "reader-----");
        let arr = []
        for (let i = 0; i < reader.length; i++) {
          let date = new Date()

          let sheetData = {
            source: reader[i]['来源'],
            userName: reader[i]['被服务人'],
            serviceName: reader[i]['服务名称'],
            address: reader[i]['服务地址'],
            workTime: reader[i]['时间分段'],
            // scheduleTime: reader[i]["服务日期"],
            /* 
							这样日期相差了六个小时啊怎么
							*/
            scheduleTime: moment(
              new Date(
                parseInt(
                  date
                    .setTime(Math.round(reader[i]['服务日期'] * 24 * 60 * 60 * 1000 - 8 * 60 * 60 * 1000) + Date.parse('1899-12-30'))
                    .toString()
                )
              )
            ).format('YYYY/MM/DD'),
            // nurseName: reader[i]["时间分段"],
            nurseName: reader[i]['护士姓名'],
            price: reader[i]['应结金额']
          }

          arr.push(sheetData)
        }
        data.importVisible = true
        data.importDataList = arr
        console.log(arr, '最后的数据是---')
      }
    }
    const confirmImport = debounce(
      async () => {
        let res = await importExcel(data.importDataList)
        if (res.success) {
          closeModal('importVisible')
          message.success('成功导入~')
          init()
        }
      },
      2000,
      true
    )

    const addNewOrder = () => {
      data.isEdit = false
      openModal('addOrderVisible')
    }
    const clear = () => {
      if (formRef.value) {
        formRef.value.resetFields()
      }
      const keys = Object.keys(form)
      let obj = {}
      keys.forEach(item => {
        obj[item] = ''
      })
      Object.assign(form, obj)

      // console.log(form.value,'==========')
    }

    const methods = {
      reset() {
        // data.selectedRowKeys = []
        clear()
        // if(formRef.value){
        //   formRef.value.resetFields()
        // }
        page.current = 1
        rowSelection.selectedRowKeys = []
        // 请求列表接口
        init()
      },
      handleFenPayOrder() {
        onKeyAllot({
          offOrderInfoDtos: [...data.fenpeiList]
        }).then(r => {
          console.log(r, '--------------')
          if (r.success) {
            data.fenpeiList = []
            data.divideAll = {
              ...r.object
            }
            data.divideAllSuccess = true
            closeModal('myFenpeiVisible')
            init()
          }
        })
      },
      handlePayOrder() {
        offlineorder({
          map: rowSelection.selectedRowKeys
        }).then(res => {
          data.myPriceVisible = false
          message.success('一键结算成功！')
          console.log(res, '00000000')
          // clear()
          rowSelection.selectedRowKeys = []
          // page.current = 1;
          init()
          // this.reset()
        })
      },
      handlePay() {
        if (rowSelection.selectedRowKeys.length == 0) {
          message.warning('请选择你要结算的订单')
          return
        }
        payOrder({
          map: rowSelection.selectedRowKeys
        }).then(res => {
          data.myPriceVisible = true
          data.payMount = res.object
        })
      },
      // 一键分配
      handleFenpei() {
        if (rowSelection.selectedRowKeys.length == 0) {
          message.warning('请选择你要分配的订单')
          return
        }
        data.fenpeiList = []
        rowSelection.selectedRowKeys.map(i => {
          data.fenpeiList.push({
            id: i
            // nurseName: i.nurseName
          })
        })
        data.myFenpeiVisible = true
      }
    }
    const editPrice = async e => {
      data.orderDetail = await initDetail(e.id)
      data.priceForm.price = e.price
      openModal('savePriceVisible')
    }
    // 打开模态框
    const openModal = e => {
      console.log('通用打开模态框', data[e])
      data[e] = true
    }
    // 关闭模态框
    const closeModal = e => {
      data[e] = false
      console.log('通用关闭模态框', data[e])
      if (e == 'addOrderVisible') {
        data[e] = false
        data.modalForm = {}
        console.log('通用关闭模态框', data[e])
      }
      if (e == 'editVisible') {
        data[e] = false
        data.editForm = {}
        console.log('通用关闭模态框', data[e])
      }
      data[e] = false
    }

    const search = () => {
      if (Object.keys(form).length == 0) {
        message.warning('请输入你要搜索的内容！！！')
        return
      }
      if (form.status == 0) {
        delete form['status']
      }

      page.current = 1
      data.loading = true
      rowSelection.selectedRowKeys = []
      init()
    }

    // 修改结算价格
    const onFinish = debounce(
      async () => {
        let obj = JSON.parse(JSON.stringify(data.orderDetail))
        obj.price = data.priceForm.price
        delete obj['key']

        let res = await editOrderPrice(obj)
        if (res.success) {
          message.success('价格已经成功修改~')
          closeModal('savePriceVisible')
          data.priceForm = {}
          init()
        }
      },
      2000,
      true
    )

    // 分配护士-护士ID
    const divideNurse = debounce(
      async key => {
        let obj = JSON.parse(JSON.stringify(data.orderDetail))
        obj.nurseId = key
        obj.status = 4 //已分配
        delete obj['key']
        console.log(obj, '分配的护士信息是')

        let res = await saveOrUpdate(obj)
        if (res.success) {
          message.success('分配成功')
          closeModal('divideNurseVisible')
          closeModal('orderVisible')
          init()
        } else {
        }
      },
      2000,
      true
    )

    const addOrder = debounce(
      () => {
        // console.log(111111111);
        data.modalFormRef.validate().then(async () => {
          console.log(data.modalForm)
          data.modalForm.scheduleTime = data.modalForm.scheduleTime.replace(/-/g, '/')
          let res = await saveOrUpdate(data.modalForm)
          if (res.success) {
            let title = data.isEdit ? '编辑' : '新增'
            message.success(`${title}成功`)
            closeModal('addOrderVisible')
            // data.addOrderVisible = false;
            init()
          }
        })
      },
      2000,
      true
    )

    const editSave = debounce(
      () => {
        let obj = JSON.parse(JSON.stringify(data.orderDetail))
        data.editFormRef.validate().then(async () => {
          obj.realPrice = data.editForm.price
          obj.status = 5
          let res = await saveOrUpdate(obj)
          if (res.success) {
            closeModal('editVisible')
            message.success('结算成功~')
            /* 其实不用请求接口了，直接修改当前项的状态和值即可算了 后续再写吧 */
            init()
          }
        })
      },
      2000,
      true
    )

    // 导出数据
    const excleData = () => {
      // 未选中--默认导出所有的数据
      if (rowSelection.selectedRowKeys.length == 0) {
        message.warning('请选择要导出的用户')
        return
      }

      const { closeModal } = useModal.confirm({
        title: `确认导出${rowSelection.selectedRowKeys.length}条数据吗?`,
        icon: createVNode(ExclamationCircleOutlined),
        onOk() {
          let obj = {
            ...data.searchForm,
            orderIds: rowSelection.selectedRowKeys
          }
          selectAllOfflineOrderExport({
            ...obj
          }).then(res => {
            console.log(res)
            // console.log(url);
            let link = document.createElement('a')
            //创建一个a标签
            link.style.display = 'none'
            //将a标签隐藏
            link.href = res.object
            //给a标签添加下载链接  "域名+接口"  safe是一个动态的域名  后面的接口替换成你自己的下载接口
            link.setAttribute('download', '线下核对')
            // 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
            document.body.appendChild(link)
            //将上面创建的a标签加入到body的尾部
            link.click()
            //执行a标签
            closeModal()
          })
        },

        class: 'test'
      })
    }

    // 自动输入
    const autoComplete = () => {
      data.editForm.price = data.orderDetail.price
    }

    // 处理订单
    const deal = async e => {
      let obj = await initDetail(e.id)
      const { closeModal } = useModal.confirm({
        title: `是否处理完成!`,
        icon: createVNode(ExclamationCircleOutlined),
        onOk: async () => {
          obj.status = 4 // 待核对
          let res = await saveOrUpdate(obj)
          if (res.success) {
            message.success('处理完成')
            init()
            closeModal()
          }
        },

        class: 'test'
      })
    }
    // 结算
    const settle = async e => {
      data.orderDetail = await initDetail(e.id)
      openModal('editVisible')
    }

    // 分配
    const divide = e => {
      data.orderDetail = e
      openModal('divideNurseVisible')
    }

    // 确认
    const confirm = e => {
      const { closeModal } = useModal.confirm({
        title: `请确保佣金核对准确并完成核对！`,
        icon: createVNode(ExclamationCircleOutlined),
        onOk: async () => {
          e.status = 6
          let res = await saveOrUpdate(e)
          if (res.success) {
            message.success('核对成功~')
            init()
            closeModal()
          }
        },

        class: 'test'
      })
    }
    // 编辑
    const edit = async e => {
      openModal('addOrderVisible')
      data.modalForm = await initDetail(e.id)
      data.modalForm.nurseId = e.nurseId
      data.isEdit = true //编辑
    }
    // 查看订单
    const look = async e => {
      data.orderDetail = await initDetail(e.id)
      data.isLook = true

      data.orderVisible = true
    }

    function initDetail(id) {
      return new Promise(async (resolve, reject) => {
        let res = await selectOne(id)
        if (res.success) {
          resolve(res.object)
        } else {
          console.log('失败11111111111')
        }
      })
    }
    const getSelectAllNewIds = obj => {
      selectAllNewIds(obj).then(res => {
        renderData.value = res.object
      })
    }
    init()

    async function init() {
      deleteObjBlank(form)
      let obj = {
        pageNum: page.current,
        pageSize: page.pageSize,
        // status: data.orderStatusAct, //订单状态
        ...form
      }
      getSelectAllNewIds(obj)
      let res = await selectAllNew(obj)
      if (res.success) {
        res.object.list.map(el => (el.key = el.number))
        res.object.list.forEach(el => (el.id = Number(el.id)))
        data.list = res.object.list
        page.total = res.object.total
        // renderData.value = res.object.allOrderIds;
        data.loading = false
      }
    }

    return {
      ...methods,
      form,
      formRef,
      colums,
      pagination,
      ...toRefs(data),
      orderStatus,
      chooseOrderStatus,
      handleTableChange,
      rowSelection,
      excleData,
      look,
      deal,
      visible,
      openModal,
      closeModal,
      search,
      labelCol: {
        style: {
          fontWeight: 'bold',
          width: '100px'
        }
      },
      rules,
      importOrder,
      addOrder,
      editOrderStatus,
      rules1,
      editSave,
      columns1,
      confirmImport,
      selectServiceTime,
      orderListStatus,
      orderListStatusColor,
      divideNurse,
      onFinish,
      settle,
      edit,
      divide,
      confirm,
      autoComplete,
      editPrice,
      addNewOrder,
      clear
    }
  },

  components: {
    CheckOutlined,
    EditOutlined,
    ExclamationCircleOutlined,
    EditTwoTone,
    DeleteTwoTone,
    ArrowUpOutlined,
    ArrowDownOutlined,
    UploadOutlined,
    divideNurse,
    EditOutlined
  }
}
</script>

<style scoped lang="less">
.search {
  background-color: #e6e6e6;
  margin-bottom: 10px;
  padding: 1.6rem;
  font-size: 1rem;

  .user-title {
    font-size: 1.5rem;
    font-weight: bold;
  }

  .user-status {
    text-align: center;
    padding: 5px 0;
    font-size: 1.25rem;
  }

  .user-status-act {
    width: 70%;

    .item-title {
      background-color: #2a9d8f;
      color: #fff;
      border-radius: 28%;
    }
  }
}
</style>
